package com.okq.service.Impl;

import com.okq.dao.ProductDao;
import com.okq.dao.ProductionDao;
import com.okq.entity.*;
import com.okq.service.IProductService;
import com.okq.util.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 描述 :
 * 作者 : zdl
 * 日期 : 2017/6/29 14:55
 */
@Transactional
@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProductionDao productionDao;

    @Override
    public void saveProduct(Product product) {
        productDao.saveProduct(product);
    }

    @Override
    public void deleteProduct(Product product) {
        productDao.deleteProduct(product);
    }

    @Override
    public void updateProduct(Product product) {
        productDao.updateProduct(product);
    }

    @Override
    public void saveOrUpdateProduct(Product product) {
        productDao.saveOrUpdateProduct(product);
    }

    @Override
    public Product getProductById(String id) {
        return productDao.getProductById(id);
    }

    @Override
    public List<Product> listAllProduct() {
        return productDao.listAllProduct();
    }

    @Override
    public Page<Product> pageByProduct(Page<Product> page, Product product) {
        if (product != null) {
            String productionIds = getProductionIdsByBatch(product.getBatch());
            if (StringUtils.isNotBlank(productionIds)) {
                product.setProductionIds(productionIds);
            }
        }
        return productDao.pageByProduct(page, product);
    }

    @Override
    public Product getProductByCode(String code) {
        return productDao.getProductByCode(code);
    }

    @Override
    public List<Product> listProductByPid(String pId) {
        return productDao.listProductByPid(pId);
    }

    @Override
    public List<ProductChart> listProductByCondition(Product product) {
        return productDao.listProductByCondition(product);
    }

    @Override
    public Page<ProductTypeNumber> pageByProductType(Page<ProductTypeNumber> page, Integer productType) {
        return productDao.pageByProductType(page, productType);
    }

    @Override
    public Page<ProductBatchNumber> pageByProductBatch(Page<ProductBatchNumber> page, Integer productType) {
        return productDao.pageByProductBatch(page, productType);
    }

    /**
     * 获取生产Ids
     *
     * @param batch 批次
     * @return STRING
     */
    private String getProductionIdsByBatch(String batch) {
        String productionIds = "";
        if (StringUtils.isNotBlank(batch)) {
            List<Production> plist = productionDao.listProductionBybatch(batch);
            if (plist != null && !plist.isEmpty() && plist.size() > 0) {
                for (Production p : plist) {
                    productionIds = p.getId() + ",";
                }
                if (StringUtils.isNotBlank(productionIds)) {
                    productionIds = productionIds.substring(0, (productionIds.length() - 1));
                }
            }
        }
        return productionIds;
    }
}
